package com.xyd.Servlet;


import com.xyd.Biz.Impl.MessageBizImpl;
import com.xyd.Biz.Impl.OrderFormBizImpl;
import com.xyd.Biz.MessageBiz;
import com.xyd.Biz.OrderFormBiz;
import com.xyd.Entity.Message;
import com.xyd.Entity.User;
import com.xyd.Util.JsonServlet;
import sun.rmi.runtime.Log;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/html/MessageServlet")
public class MessageServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String op = req.getParameter("op");
        if (op.equals("insert")) {
            insert(req, resp);
        } else if (op.equals("PLinsert")) {
            PLinsert(req, resp);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    public void insert(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        MessageBiz messageBiz = new MessageBizImpl();
        String guestContent = req.getParameter("guestContent");
        Message message = new Message();
        message.setContext(guestContent);
        message.setTime(new Date().toLocaleString());
        User user = (User) req.getSession().getAttribute("user");
        message.setUserid(user.getUserid());
        message.setCommodity_id(String.valueOf(0));
        message.setVal_type(1);
        int flag = messageBiz.insertMsgByMsg(message);
        if (flag > 0) {
            resp.sendRedirect("../guestbook.jsp");
        }
    }

    public void PLinsert(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.setContentType("text/html;charset=UTF-8");
        MessageBiz messageBiz = new MessageBizImpl();
        String guestContent = req.getParameter("guestContent");
        String id = req.getParameter("id");
        String orderid = req.getParameter("orderid");
        OrderFormBiz orderFormBiz = new OrderFormBizImpl();
        Message message = new Message();
        message.setContext(guestContent);
        message.setTime(new Date().toLocaleString());
        User user = (User) req.getSession().getAttribute("user");
        message.setUserid(user.getUserid());
        message.setCommodity_id(String.valueOf(id));
        message.setVal_type(1);
        int flag = messageBiz.insertMsgByMsg(message);
        if (flag > 0) {
            orderFormBiz.updateSonOrderState(4, Integer.parseInt(orderid));
            resp.getWriter().write("<script>alert('评论完成！');location.href='/shopping/html/ShowOrderServlet';</script>");
        } else {
            resp.getWriter().write("<script>alert('评论失败！');location.href='/shopping/html/ShowOrderServlet';</script>");
        }
    }
}
